/********************************************************************
	created:	2008/09/29
	created:	29:9:2008   17:16
	filename: 	h:\SimoneEngine\qianye\src\qylib\lex\qyLex.cpp
	author:		Xiong Jiayu
	copyright:  
	purpose:	
*********************************************************************/

#include "../common/qyCommon.h"
#include "qyLex.h"
#include <stdio.h>

void qyLex_GetInts( const int numInts, int *pData, qyTokenIterator &tok_it )
{
	if ( pData == NULL || numInts == 0 )
		return;
	for ( int i = 0; i < numInts; i++ )
	{
		int temp;
		sscanf( (*tok_it).c_str(), "%d", &temp );
		pData[i] = temp;
		++tok_it;
	} 
}

void qyLex_GetFloats( const int numFloats, float *pData, qyTokenIterator &tok_it )
{
	if ( pData == NULL || numFloats == 0 )
		return;
	for ( int i = 0; i < numFloats; i++ )
	{
		float temp;
		sscanf( (*tok_it).c_str(), "%f", &temp );
		pData[i] = temp;
		++tok_it;
	}
}

int qyLex_GetNumNodeInToken( qyTokenizer &tokenizer )
{
	qyTokenIterator end = tokenizer.end();
	int numNodes = 0;
	for ( qyTokenIterator it = tokenizer.begin(); it != end; ++it )
	{
		numNodes++;
	}
	return numNodes;
}

//-----------------------------------------------------------------------
void qyMatrix4_to_string( const Matrix4 &mat, qyString &text )
{
    char buf[256];
    sprintf( buf, "%f %f %f %f ", mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
    text += buf;
    sprintf( buf, "%f %f %f %f ", mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
    text += buf;
    sprintf( buf, "%f %f %f %f ", mat[2][0], mat[2][1], mat[2][2], mat[2][3] );
    text += buf;
    sprintf( buf, "%f %f %f %f", mat[3][0], mat[3][1], mat[3][2], mat[3][3] );
    text += buf;
}

//-----------------------------------------------------------------------
void qyMatrix4_from_string( Matrix4 &mat, const qyString &text )
{
    qyTokenSep sep( "\r\n\t, " );
    qyTokenizer tokenizer( text, sep );
    qyTokenIterator tok_it = tokenizer.begin();

    qyLex_GetFloats( 16, mat.ToFloatPtr(), tok_it );
}

//-----------------------------------------------------------------------
void qyQuatTS_from_string( QuatTS &qts, const qyString &text )
{
	qyTokenSep sep( "\r\n\t, " );
	qyTokenizer tokenizer( text, sep );
	qyTokenIterator tok_it = tokenizer.begin();
	float fArray[10];
	qyLex_GetFloats( 10, fArray, tok_it );
	qts.q.Set( fArray[0], fArray[1], fArray[2], fArray[3] );
	qts.t.Set( fArray[4], fArray[5], fArray[6] );
	qts.s.Set( fArray[7], fArray[8], fArray[9] );
}

//-----------------------------------------------------------------------
void qyQuatTS_to_string( const QuatTS &qts, qyString &text )
{
	char buf[256];
	sprintf( buf, "%f %f %f %f ", qts.q[0], qts.q[1], qts.q[2], qts.q[3] );
	text += buf;
	sprintf( buf, "%f %f %f ", qts.t[0], qts.t[1], qts.t[2] );
	text += buf;
	sprintf( buf, "%f %f %f", qts.s[0], qts.s[1], qts.s[2] );
	text += buf;
}

//-----------------------------------------------------------------------
void qyXml_SetAttribute( TiXmlElement *node, const char *name, const bool &value )
{
    char buf[32];
    int i = value ? 1 : 0;
    sprintf( buf, "%d", i );
    node->SetAttribute( name, buf );
}

//-----------------------------------------------------------------------
void qyXml_GetAttribute( TiXmlElement *node, const char *name, bool &value )
{
    int i;
    const char *buf = node->Attribute( name );
	if ( buf == NULL )
	{	
		value = false;
		return;
	}
    sscanf( buf, "%d", &i );
    value = (i==1);
}


//-----------------------------------------------------------------------
void qyXml_SetAttribute( TiXmlElement *node, const char *name, const int &value )
{
    char buf[32];
    sprintf( buf, "%d", value );
    node->SetAttribute( name, buf );
}

//-----------------------------------------------------------------------
void qyXml_GetAttribute( TiXmlElement *node, const char *name, int &value )
{
    const char *buf = node->Attribute( name );
	if ( buf == NULL )
	{	
		value = 0;
		return;
	}
    sscanf( buf, "%d", &value );
}


//-----------------------------------------------------------------------
void qyXml_SetAttribute( TiXmlElement *node, const char *name, const float &value )
{
    char buf[32];
    sprintf( buf, "%f", value );
    node->SetAttribute( name, buf );
}

//-----------------------------------------------------------------------
void qyXml_GetAttribute( TiXmlElement *node, const char *name, float &value )
{
    const char *buf = node->Attribute( name );
	if ( buf == NULL )
	{	
		value = 0;
		return;
	}
    sscanf( buf, "%f", &value );
}

//-----------------------------------------------------------------------
void qyXml_SetAttribute( TiXmlElement *node, const char *name, const Vector3 &value )
{
    char buf[128];
    sprintf( buf, "%f %f %f", value.x, value.y, value.z );
    node->SetAttribute( name, buf );
}
//-----------------------------------------------------------------------
void qyXml_GetAttribute( TiXmlElement *node, const char *name, Vector3 &value )
{
    const char *buf = node->Attribute( name );
	if ( buf == NULL )
	{	
		value.Set( 0, 0, 0 );
		return;
	}
    sscanf( buf, "%f %f %f", &value.x, &value.y, &value.z );
}

//-----------------------------------------------------------------------
void qyXml_SetAttribute( TiXmlElement *node, const char *name, const Vector4 &value )
{
	char buf[128];
	sprintf( buf, "%f %f %f %f", value.x, value.y, value.z, value.w );
	node->SetAttribute( name, buf );
}

//-----------------------------------------------------------------------
void qyXml_GetAttribute( TiXmlElement *node, const char *name, Vector4 &value )
{
	const char *buf = node->Attribute( name );
	if ( buf == NULL )
	{	
		value.Set( 0, 0, 0, 0 );
		return;
	}
	sscanf( buf, "%f %f %f %f", &value.x, &value.y, &value.z, &value.w );
}

//-----------------------------------------------------------------------
void qyXml_SetAttribute( TiXmlElement *node, const char *name, const qyColor &value )
{
	char buf[128];
	sprintf( buf, "%f %f %f %f", value.r, value.g, value.b, value.a );
	node->SetAttribute( name, buf );
}

//-----------------------------------------------------------------------
void qyXml_GetAttribute( TiXmlElement *node, const char *name, qyColor &value )
{
	const char *buf = node->Attribute( name );
	if ( buf == NULL )
	{	
		value.Set( 0, 0, 0, 0 );
		return;
	}
	sscanf( buf, "%f %f %f %f", &value.r, &value.g, &value.b, &value.a );
}

//-----------------------------------------------------------------------
void qyXml_SetAttribute( TiXmlElement *node, const char *name, const AABB &value )
{
    char buf[256];
    sprintf( buf, "%f %f %f %f %f %f", 
        value.GetMin().x, value.GetMin().y, value.GetMin().z, 
        value.GetMax().x, value.GetMax().y, value.GetMax().z );
    node->SetAttribute( name, buf );
}

//-----------------------------------------------------------------------
void qyXml_GetAttribute( TiXmlElement *node, const char *name, AABB &value )
{
    const char *buf = node->Attribute( name );
	if ( buf == NULL )
	{	
		return;
	}
    sscanf( buf, "%f %f %f %f %f %f", 
        &value.vecs[0].x, &value.vecs[0].y, &value.vecs[0].z, 
        &value.vecs[1].x, &value.vecs[1].y, &value.vecs[1].z 
        );
}

//-----------------------------------------------------------------------
void qyXml_SetAttribute( TiXmlElement *node, const char *name, const Matrix4 &value )
{
	qyString buf;
	qyMatrix4_to_string( value, buf );
	node->SetAttribute( name, buf.c_str() );
}

//-----------------------------------------------------------------------
void qyXml_GetAttribute( TiXmlElement *node, const char *name, Matrix4 &value )
{
	const char *buf = node->Attribute( name );
	if (  buf == NULL )
	{
		return;
	}
	qyString str( buf );
	qyMatrix4_from_string( value, str );
}

//-----------------------------------------------------------------------
void qyXml_SetAttribute( TiXmlElement *node, const char *name, const QuatTS &value )
{
	qyString buf;
	qyQuatTS_to_string( value, buf );
	node->SetAttribute( name, buf.c_str() );
}

//-----------------------------------------------------------------------
void qyXml_GetAttribute( TiXmlElement *node, const char *name, QuatTS &value )
{
	const char *buf = node->Attribute( name );
	if (  buf == NULL )
	{
		return;
	}
	qyString str( buf );
	qyQuatTS_from_string( value, str.c_str() );
}

